import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.util.ArrayList;
import java.util.Random;


public class DrawPanel extends Panel{
	
	
	private ArrayList<Graphs> _gs = new ArrayList<Graphs>();
	private ArrayList<Color> _color = new ArrayList<Color>();

	
	public DrawPanel(ArrayList<Graphs> gs, ArrayList<Color> color) {
		_gs = gs;
		_color = color;
		repaint();
	}

	@Override
	public void paint(Graphics g) {


		final int SPACE = 30;
		final int SPLITTED = 10;
		final int SPLITTEDY = 5; 
		int spaceNumbers = 0;
		int spaceToLine = 15;
		int space = 0;
		int length = 5;
		Integer i = 0;

		g.drawLine(getWidth() / 2, SPACE, getWidth() / 2, getHeight() - SPACE);
		g.drawLine(SPACE, getHeight() / 2, getWidth() - SPACE, getHeight() / 2);

		while ((getWidth() / 2 - space) - SPACE >= 0) {
			spaceNumbers = (getWidth()/2) / SPLITTED;
			g.drawString(i.toString(), getWidth() / 2 + space,
					(getHeight() / 2) + spaceToLine);
			Integer ineg = i * (-1);
			g.drawString(ineg.toString(), getWidth() / 2 - space,
					(getHeight() / 2) + spaceToLine);
			g.drawLine(getWidth() / 2 + space, getHeight() / 2, getWidth() / 2
					+ space, (getHeight() / 2) + length);
			g.drawLine(getWidth() / 2 - space, getHeight() / 2, getWidth() / 2
					- space, (getHeight() / 2) + length);
			i = i + 2;
			space = space + spaceNumbers;
		}
		space = 0;
		space = space + spaceNumbers;
		i = 20;

		while ((getHeight() / 2 - space) - SPACE >= 0) {
			spaceNumbers = (getHeight()/2) / SPLITTEDY;
			Integer ineg = i * (-1);
			g.drawString(ineg.toString(), (getWidth() / 2) + spaceToLine,
					getHeight() / 2 + space);

			g.drawString(i.toString(), (getWidth() / 2) + spaceToLine,
					getHeight() / 2 - space);
			g.drawLine(getWidth() / 2, getHeight() / 2 + space,
					(getWidth() / 2) + length, getHeight() / 2 + space);
			g.drawLine(getWidth() / 2, getHeight() / 2 - space,
					(getWidth() / 2) + length, getHeight() / 2 - space);
			i = i + 20;

			space = space + spaceNumbers;
		}
		if (_gs != null) {
			
			for (Graphs dp : _gs) {
				double pixelx = ( (getWidth()/2 / SPLITTED) / 2);
				double pixely = ( (getHeight()/2 / SPLITTEDY) / 20);
				double xold = 0;
				double yold = 0;
				double y = 0;
				g.setColor(_color.get(dp._linesCount));
				((Graphics2D)g).setStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
			
				for (double x = -50 ; x <= 50; x = x + 0.1) {

					y = dp._a *x * x+ dp._b * x + dp._c;
					if(x == - 50){
						xold = x; 
						yold = y;
					}

					g.drawLine((int) (getWidth()/ 2 - xold * pixelx), (int)( getHeight()/ 2 - yold * pixely),
							(int) (getWidth()/2 - x * pixelx), (int)( getHeight()/2 - y * pixely));
					xold = x;
					yold = y;
				}
			}
		}
	}
}
